package com.qianhui.bean;

import com.qianhui.common.CM;
import com.qianhui.common.CP;
import com.qianhui.common.IStatus;
import com.qianhui.dao.*;
import com.qianhui.pojo.*;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class BatchTaskJob_OneHour implements Job
{

    private GoodsDAO goodsDAO = new GoodsDAO();
    private OrderGoodsDAO orderGoodsDAO = new OrderGoodsDAO();
    private CommentDAO commentDAO = new CommentDAO();
    private BuyerDAO buyerDAO = new BuyerDAO();
    private OrderDAO orderDAO = new OrderDAO();

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException
    {
        System.out.println(CM.getTimeStrNow() + "每小时半点的批量任务执行--开始");
        batch_process_order_to_be_payed();
        batch_process_order_to_be_received();
        batch_process_order_to_be_commented();
        System.out.println(CM.getTimeStrNow() + "每小时半点的批量任务执行--结束");
    }

    private void batch_process_order_to_be_payed()
    {
        System.out.println("批量处理待付款订单--开始");
        // 批量计算全网销售中商品的销量和评价数
        int allOrderCountInDB = orderDAO.getTotalCountByStatusAnd_keyword(IStatus.ORDER_TO_BE_PAYED, null);
        int onePageSize = 10;
        if (allOrderCountInDB > 0)
        {
            int ttpTemp = allOrderCountInDB % onePageSize == 0 ? allOrderCountInDB / onePageSize : allOrderCountInDB / onePageSize + 1;
            for (int i = 1; i <= ttpTemp; i++)
            {
                List<Order> orderList = orderDAO.getListByIndexAndPageSizeAndStatusAnd_keyword(i, onePageSize, IStatus.ORDER_TO_BE_PAYED, null);
                if (orderList != null && orderList.size() > 0)
                {
                    for (Order oTemp : orderList)
                    {
                        // 创建订单到现在在22~24小时内提醒用户付款
                        Calendar c_now = Calendar.getInstance();
                        Calendar c_order = Calendar.getInstance();
                        c_order.setTime(oTemp.getOrderBuyTime());
                        c_order.add(Calendar.HOUR_OF_DAY, 22);
                        Date date_22 = c_order.getTime();
                        c_order.add(Calendar.HOUR_OF_DAY, 2);
                        Date date_24 = c_order.getTime();
                        if (c_now.getTime().after(date_22) && date_24.after(c_now.getTime()))
                        {
                            // 给用户发送付款提醒
                            Buyer buyerFromDB = buyerDAO.findById(oTemp.getBuyerId());
                            if (buyerFromDB != null)
                            {
                                CP.push_order_not_pay_info(buyerFromDB.getBuyerWechat(), oTemp);
                            }
                        }
                        // 创建订单到现在24小时后仍未付款取消订单
                        if (c_now.getTime().after(date_24))
                        {
                            List<Order> orderListTemp = new ArrayList<Order>();
                            orderListTemp.add(oTemp);

                            List<Goods> goodsList = new ArrayList<Goods>();
                            for (Order ordTemp : orderListTemp)
                            {
                                if (ordTemp.getOrderStatus().equals(IStatus.ORDER_TO_BE_PAYED))
                                {
                                    ordTemp.setOrderStatus(IStatus.ORDER_HAVE_BEEN_CANCELLED);

                                    List<OrderGoods> orderGoodsList = orderGoodsDAO.findByOrderId(ordTemp.getOrderId());
                                    for (OrderGoods orderGoodsTemp : orderGoodsList)
                                    {
                                        Goods goodsTemp = goodsDAO.findById(orderGoodsTemp.getGoodsId());
                                        goodsTemp.setGoodsInventoryNumber(goodsTemp.getGoodsInventoryNumber() + orderGoodsTemp.getGoodsNumber());
                                        goodsList.add(goodsTemp);
                                    }
                                }
                            }

                            List<Order> orderListResult = orderDAO.batchCancelOrder(orderListTemp, goodsList);
                            if (orderListResult != null)
                            {
                                for (Order resOrder : orderListResult)
                                {
                                    // 给用户发送取消订单通知
                                    Buyer buyerFromDB = buyerDAO.findById(oTemp.getBuyerId());
                                    if (buyerFromDB != null)
                                    {
                                        CP.push_order_have_been_cancelled(buyerFromDB.getBuyerWechat(), resOrder);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        System.out.println("批量处理待付款订单--结束");
    }

    private void batch_process_order_to_be_received()
    {
        System.out.println("批量处理待收货订单--开始");
        int allOrderCountInDB = orderDAO.getTotalCountByStatusAnd_keyword(IStatus.ORDER_TO_BE_RECEIVED, null);
        int onePageSize = 10;
        if (allOrderCountInDB > 0)
        {
            int ttpTemp = allOrderCountInDB % onePageSize == 0 ? allOrderCountInDB / onePageSize : allOrderCountInDB / onePageSize + 1;
            for (int i = 1; i <= ttpTemp; i++)
            {
                List<Order> orderList = orderDAO.getListByIndexAndPageSizeAndStatusAnd_keyword(i, onePageSize, IStatus.ORDER_TO_BE_RECEIVED, null);
                if (orderList != null && orderList.size() > 0)
                {
                    for (Order oTemp : orderList)
                    {
                        if (oTemp.getOrderSendTime() == null)
                        {
                            continue;
                        }

                        // 发货10天后还没有确认收货的系统自动确认收货
                        Calendar c_now = Calendar.getInstance();
                        Calendar c_order = Calendar.getInstance();
                        c_order.setTime(oTemp.getOrderSendTime());
                        c_order.add(Calendar.DAY_OF_MONTH, 10);
                        Date date_10 = c_order.getTime();
                        if (c_now.getTime().after(date_10))
                        {
                            oTemp.setOrderStatus(IStatus.ORDER_TO_BE_COMMENTED);
                            oTemp.setOrderReceiveTime(new Timestamp(Calendar.getInstance().getTimeInMillis()));
                            orderDAO.save(oTemp);
                        }

                    }
                }
            }
        }
        System.out.println("批量处理待收货订单--结束");
    }

    private void batch_process_order_to_be_commented()
    {
        System.out.println("批量处理待评价订单--开始");
        int allOrderCountInDB = orderDAO.getTotalCountByStatusAnd_keyword(IStatus.ORDER_TO_BE_COMMENTED, null);
        int onePageSize = 10;
        if (allOrderCountInDB > 0)
        {
            int ttpTemp = allOrderCountInDB % onePageSize == 0 ? allOrderCountInDB / onePageSize : allOrderCountInDB / onePageSize + 1;
            for (int i = 1; i <= ttpTemp; i++)
            {
                List<Order> orderList = orderDAO.getListByIndexAndPageSizeAndStatusAnd_keyword(i, onePageSize, IStatus.ORDER_TO_BE_COMMENTED, null);
                if (orderList != null && orderList.size() > 0)
                {
                    for (Order oTemp : orderList)
                    {
                        if (oTemp.getOrderReceiveTime() == null)
                        {
                            continue;
                        }

                        // 确认收货后3天的系统自动填写评价
                        Calendar c_now = Calendar.getInstance();
                        Calendar c_order = Calendar.getInstance();
                        c_order.setTime(oTemp.getOrderReceiveTime());
                        c_order.add(Calendar.DAY_OF_MONTH, 3);
                        Date date_15 = c_order.getTime();
                        if (c_now.getTime().after(date_15))
                        {

                            List<OrderGoods> orderGoodsList = orderGoodsDAO.findByProperty(OrderGoodsDAO.ORDER_ID, oTemp.getOrderId());

                            List<Comment> commentListTemp = new ArrayList<Comment>();
                            for (OrderGoods orderGoods : orderGoodsList)
                            {
                                Comment commentTemp = new Comment();
                                commentTemp.setCommentId(CM.getRandomTwentyFivePrimaryKeyId());
                                commentTemp.setCommentDescriptionStar(5);
                                commentTemp.setCommentServiceStar(5);
                                commentTemp.setCommentDeliverStar(5);
                                commentTemp.setCommentLogisticsStar(5);
                                commentTemp.setCommentCourierStar(5);
                                commentTemp.setCommentContent("此用户没有填写评论");
                                commentTemp.setCommentContentTime(new Timestamp(Calendar.getInstance().getTimeInMillis()));
                                commentTemp.setBuyerId(oTemp.getBuyerId());
                                commentTemp.setGoodsId(orderGoods.getGoodsId());
                                commentTemp.setOrderId(oTemp.getOrderId());

                                commentListTemp.add(commentTemp);
                                orderGoods.setCommentId(commentTemp.getCommentId());
                            }
                            oTemp.setOrderStatus(IStatus.ORDER_HAVE_BEEN_COMPLETED);
                            oTemp.setOrderCompleteTime(new Timestamp(Calendar.getInstance().getTimeInMillis()));

                            Order post_order_result = orderDAO.post_order_and_post_comments(oTemp, orderGoodsList, commentListTemp);

                        }

                    }
                }
            }
        }
        System.out.println("批量处理待评价订单--结束");
    }

}
